﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace InSys.Documents
{
    public partial class UCSearchDocument : UserControl
    {
        public UCSearchDocument()
        {
            InitializeComponent();
            clear();
            kladrEdit1.SqlConnectionString = Properties.Settings.Default.testlinqConnectionString;
        }

        public event EventHandler ButtonSearchClickEvent;

        private void FireButtonsearchClickEvent()
        {
            if (null == ButtonSearchClickEvent) return;
            EventArgs e = new EventArgs();
            ButtonSearchClickEvent(this, e);
        }


        private MapTestLinqDataContext db;
        private string registryNumber;
        private DateTime registryDate;
        private string content;
        private string enterprise;
        private int internalNumber;
        private string localID;
        private bool notUsed;
        private int userID;
        private string address;
        private int kindDocumentID;
        private bool notUsedKindDocument;
        private string extRegistryNumber;
        private DateTime extRegistryDate;

        public string RegistryNumber { get { return registryNumber; } set { registryNumber = value; regNumTextBox.Text = registryNumber; } }
        public DateTime RegistryDate { get { return registryDate; } set { registryDate = value; dateTimePicker1.Value = registryDate; } }
        public string Content { get { return content; } set { content = value; ContentTextBox.Text = content; } }
        public string Enterprise { get { return enterprise; } set { enterprise = value; EnterpriseTextBox.Text = enterprise; } }
        public int InternalNumber { get { return internalNumber; } set { internalNumber = value; InternalNumberTextBox.Text = internalNumber.ToString(); } }
        public string LocalID { get { return localID; } set { localID = value; LocalIDTextBox.Text = localID.ToString(); } }
        public bool NotUsed { get { return notUsed; } set { notUsed = value; if (notUsed == true) NotUsePersonCheckBox.CheckState = CheckState.Checked; else  NotUsePersonCheckBox.CheckState = CheckState.Unchecked; } }
        public int UserID
        {
            get { return userID; }
            set
            {
                userID = value;
                PersonComboBox.SelectedValue = userID;
            }
        }

        public int KindDocumentID
        {
            get { return kindDocumentID; }
            set
            {
                kindDocumentID = value;
                kindDocumentComboBox.SelectedValue = kindDocumentID;
            }
        }
        public bool NotUsedKindDocument
        {
            get { return notUsedKindDocument; }
            set { notUsedKindDocument = value; notUseKindDocumentCheckbox.Checked = notUsedKindDocument; }
        }
        public string Address
        {
            get
            {
                if (null == address)
                    address = "";
                return address;
            }
            set
            {
                address = value;
                if (null == address) address = "";
            }
        }

        public string ExtRegistryNumber { get { return extRegistryNumber; } set { extRegistryNumber = value; extRegNumberTextBox.Text = extRegistryNumber; } }
        public DateTime ExtRegistryDate
        {
            get
            {
                return extRegistryDate;
            }
            set
            {
                extRegistryDate = value;
                ExtRegDateTimePicker.Value = extRegistryDate;
            }
        }


        private void clear()
        {
            RegistryNumber = "";
            RegistryDate = DateTime.Today;
            Content = "";
            Enterprise = "";
            InternalNumber = 0;
            LocalID = "";
            NotUsed = true;
            address = "";
            NotUsedKindDocument = true;
            db = new MapTestLinqDataContext();
            db.ObjectTrackingEnabled = false;
            PersonComboBox.DataSource = from item in db.persons orderby item.name select item;
            if (false == notUsed)
                PersonComboBox.SelectedValue = userID;
            kindDocumentBindingSource.DataSource = from kd in db.kindDocument orderby kd.kind_document select kd;
            extRegistryNumber = "";
            ExtRegistryDate = new DateTime(1899, 12, 30);
        }

        private void buttonSearch_Click(object sender, EventArgs e)
        {
            registryNumber = regNumTextBox.Text;
            registryDate = dateTimePicker1.Value;
            content = ContentTextBox.Text;
            enterprise = EnterpriseTextBox.Text;
            try
            {
                internalNumber = Convert.ToInt32(InternalNumberTextBox.Text);
            }
            catch (Exception)
            {
                internalNumber = 0;
            }

            try
            {
                localID = LocalIDTextBox.Text.Trim();
            }
            catch (Exception)
            {
                localID = "";
            }
            notUsed = NotUsePersonCheckBox.CheckState == CheckState.Checked;
            notUsedKindDocument = notUseKindDocumentCheckbox.CheckState == CheckState.Checked;
            //address = AddressTextBox.Text;
            address = kladrEdit1.Address;
            try
            {
                userID = (int)PersonComboBox.SelectedValue;
            }
            catch (Exception)
            {
                userID = -1;
            }
            try
            {
                kindDocumentID = (int)kindDocumentComboBox.SelectedValue;
            }
            catch (Exception)
            {
                kindDocumentID = -1;
            }
            extRegistryDate = ExtRegDateTimePicker.Value;
            registryDate = dateTimePicker1.Value;
            ExtRegistryNumber = extRegNumberTextBox.Text.Trim();
            FireButtonsearchClickEvent();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            clear();
        }



        private void NotUsePersonCheckBox_CheckStateChanged(object sender, EventArgs e)
        {
            PersonComboBox.Enabled = !(NotUsePersonCheckBox.CheckState == CheckState.Checked);
        }

        private void notUseKindDocumentCheckbox_CheckStateChanged(object sender, EventArgs e)
        {
            kindDocumentComboBox.Enabled = !(notUseKindDocumentCheckbox.CheckState == CheckState.Checked);
        }
    }
}
